
/*---------------------------------------------------------
   Name: 
---------------------------------------------------------*/
local panel= nil
local percent=0
function PANEL:Init()
panel = self

	self.Panel2 = vgui.Create( "DPanel", self )
	self.UploadButton2 = vgui.Create( "DButton", self.Panel2)
	self.UploadButton2:SetSize( 75, 50)
	self.UploadButton2:SetText( "Upload" )
	self.UploadButton2.DoClick = function() self:CheckFile() end
	
	self.CancelButton2 = vgui.Create( "DButton", self.Panel2)
	self.CancelButton2:SetSize(75,50)
	self.CancelButton2:SetText("Cancel")
	self.CancelButton2.DoClick = function() self.Panel2:SetVisible(false) self.Panel:SetVisible(true)  end
	
	self.DeleteButton2 = vgui.Create( "DButton", self.Panel2)
	self.DeleteButton2:SetSize(75,50)
	self.DeleteButton2:SetText("Delete")
	self.DeleteButton2.DoClick = function() self:DeleteFile("client") end
	

	self.List2 = vgui.Create( "DListView", self.Panel2 )
	self.List2:SetMultiSelect(false)
	self.List2:AddColumn("Files")
	self.List2.DoDoubleClick = function(parent, index, list)
	self:CheckFile()
	end
	
	self.Panel = vgui.Create( "DPanel", self )
	
	self.NewName = vgui.Create( "DTextEntry", self.Panel )
	self.NewName:SetKeyboardInputEnabled( true )
	self.NewName:SetEnabled( true )
	
	self.StoreButton = vgui.Create( "DButton", self.Panel )
	self.StoreButton:SetSize( 60, 25 )
	self.StoreButton:SetText( "Save" )
	self.StoreButton.DoClick = function() self:OnStore() end
	
	self.OpenButton = vgui.Create( "DButton", self.Panel )
	self.OpenButton:SetSize(75,50)
	self.OpenButton:SetText("Open")
	self.OpenButton.DoClick = function() self:CheckFile2() end
	
	self.FolderButton = vgui.Create( "DButton", self.Panel )
	self.FolderButton:SetSize(75,50)
	self.FolderButton:SetText("New Folder")
	self.FolderButton.DoClick = function() self:NewFolder() end
	
	self.DeleteButton = vgui.Create( "DButton", self.Panel )
	self.DeleteButton:SetSize(75,50)
	self.DeleteButton:SetText("Delete")
	self.DeleteButton.DoClick = function() self:DeleteFile("server") end
	
	self.UploadButton = vgui.Create( "DButton", self.Panel )
	self.UploadButton:SetSize(75,50)
	self.UploadButton:SetText("Upload")
	self.UploadButton.DoClick = function() self.Panel:SetVisible(false) self.Path = "tbsduplicator" self:PopulateUpload("tbsduplicator") self.Panel2:SetVisible(true) end
	
	self.DownloadButton = vgui.Create( "DButton", self.Panel )
	self.DownloadButton:SetSize(75,50)
	self.DownloadButton:SetText("Download")
	self.DownloadButton.DoClick = function() if self.List:GetSelected()[1]!=nil then RunConsoleCommand("tbdupe_InitializeFile",self.List:GetSelected()[1]:GetValue(1)) end end

	
	self.List = vgui.Create( "DListView", self.Panel  )
	self.List:SetMultiSelect(false)
	self.List:AddColumn("Files")
	self.List.DoDoubleClick = function(parent, index, list)
	self:CheckFile2()
	end
	
	self.DoubleCheck = vgui.Create( "DFrame" )
	self.DoubleCheck:SetTitle( "Are You Sure?" )
	self.DoubleCheck:ShowCloseButton( true )
	self.DoubleCheck:SetSize(250, 100)
	self.DoubleCheck:Center()
	self.DoubleCheck:ShowCloseButton(false)
	self.DoubleCheck:SetVisible(false)
	
	
	self.Msg = vgui.Create("DLabel", self.DoubleCheck)
	self.Msg:AlignLeft(5)
	self.Msg:AlignTop(30)
	
	self.ConfirmButton = vgui.Create("DButton", self.DoubleCheck)
	self.ConfirmButton:SetSize(75,25)
	self.ConfirmButton:SetText("Delete")	
	self.ConfirmButton:AlignBottom(5)
	self.ConfirmButton:AlignLeft(10)
	self.ConfirmButton.DoClick = function() self:DeleteFile2() end
	
	self.CancelButton = vgui.Create("DButton", self.DoubleCheck)
	self.CancelButton:SetSize(75,25)
	self.CancelButton:SetText("Cancel")	
	self.CancelButton:AlignBottom(5)
	self.CancelButton:AlignRight(10)
	self.CancelButton.DoClick = function() self.DoubleCheck:SetVisible(false) end
	
	self.DuplicateList = {}
	self.FileToStream = ""
	self.FileToBeWritten=""
	self.FileName=""
	self.Path = ""
	self.Path2 = ""
end

local function Done()
	LocalPlayer():ChatPrint("DUPLICATOR: Upload Finished.")
	panel.Panel2:SetVisible(false)
	panel.Panel:SetVisible(true)
	panel.List:SortByColumn(1)
end



/*---------------------------------------------------------
   Name: OnStore
---------------------------------------------------------*/
function PANEL:OnStore()
	
	if(self.NewName:GetValue()!="")then
		local name = self.NewName:GetValue()
		
		local newpath = string.Explode("/", self.Path2)

		local max = #newpath
		local path = self.Path2							
			if(string.find(newpath[max], ".txt"))then
				path=""
					for k,v in pairs(newpath)do
						if(k<max)then
							if(k==max-1)then
								path = path..v
							else
								path = path..v.."/"
							end
						end
					end
			end

		RunConsoleCommand( "tbdupe_SaveDuplication", name, path )
	end
	
end

function PANEL:NewFolder()
	
	if(self.NewName:GetValue()!="")then
		local name = self.NewName:GetValue()
		
		local newpath = string.Explode("/", self.Path2)

		local max = #newpath
		local path = self.Path2							
			if(string.find(newpath[max], ".txt"))then
				path=""
					for k,v in pairs(newpath)do
						if(k<max)then
							if(k==max-1)then
								path = path..v
							else
								path = path..v.."/"
							end
						end
					end
			end

		RunConsoleCommand( "tbdupe_NewFolder", name, path )
	end
	
end


function PANEL:DeleteFile(where)
	
	if(where=="server")then
		
		if self.List:GetSelected()[1]!=nil then
			self.Msg:SetText([[Are you sure you want to delete "]]..self.List:GetSelected()[1]:GetValue(1)..[["?]])
			self.Msg:SizeToContents()
			self.selected = self.List:GetSelected()[1]:GetValue(1)
			self.line = self.List:GetSelectedLine()
			self.DoubleCheck.where = where
			self.DoubleCheck:SetVisible(true)
			self.DoubleCheck:MakePopup()
		end
		
	else
		
		if self.List2:GetSelected()[1]!=nil then
			self.Msg:SetText([[Are you sure you want to delete "]]..self.List2:GetSelected()[1]:GetValue(1)..[["?]])
			self.Msg:SizeToContents()
			self.selected = self.List2:GetSelected()[1]:GetValue(1)
			self.line = self.List2:GetSelectedLine()
			self.DoubleCheck.where = where
			self.DoubleCheck:SetVisible(true)
			self.DoubleCheck:MakePopup()
		end
		
	end
end

function PANEL:DeleteFile2()
	
	
	if(self.DoubleCheck.where=="server")then
	
		local newpath = string.Explode("/", self.Path2)

		local max = #newpath
		local path = self.Path2							
			if(string.find(newpath[max], ".txt"))then
				path=""
					for k,v in pairs(newpath)do
						if(k<max)then
							if(k==max-1)then
								path = path..v
							else
								path = path..v.."/"
							end
						end
					end
			end
	
		self.DoubleCheck:SetVisible(false)
		RunConsoleCommand("tbdupe_DoDeleteFile", self.selected, path)
		self.List:RemoveLine(self.line)
	else
	
		local newpath = string.Explode("/", self.Path2)

		local max = #newpath
		local path = self.Path							
			if(string.find(newpath[max], ".txt"))then
				path=""
					for k,v in pairs(newpath)do
						if(k<max)then
							if(k==max-1)then
								path = path..v
							else
								path = path..v.."/"
							end
						end
					end
			end
	
			self.DoubleCheck:SetVisible(false)
			file.Delete(path.."/"..self.selected..".txt")
			self:PopulateUpload(self.Path)
	end
end

function PANEL:CheckFile()

				if self.List2:GetSelected()[1]!=nil then
					local txtfile = self.List2:GetSelected()[1]:GetValue(1)
					
						if(string.find(txtfile, "/") or string.find(txtfile, [[\]]))then

								if(string.find(txtfile, [[\]]))then
								
									local newpath = string.Explode("/", self.Path)
									self.Path=""
									
									local max = #newpath
									
										if(string.find(newpath[max], ".txt"))then
											max = max-1
										end
										
										for k,v in pairs(newpath)do
											if(k<max)then
												if(k==max-1)then
													self.Path = self.Path..v
												else
													self.Path = self.Path..v.."/"
												end
											end
										end
									
									self:PopulateUpload(self.Path)
								else
									txtfile = string.Left(txtfile, string.len(txtfile)-3)
									self.Path = self.Path..txtfile
									self:PopulateUpload(self.Path)
								end
						else
								
								local newpath = string.Explode("/", self.Path)

									local max = #newpath

										if(string.find(newpath[max], ".txt"))then
											self.Path=""
												for k,v in pairs(newpath)do
													if(k<max)then
														if(k==max-1)then
															self.Path = self.Path..v
														else
															self.Path = self.Path..v.."/"
														end
													end
												end
										end
								
								self.Path = self.Path.."/"..txtfile..".txt"
								RunConsoleCommand("tbdupe_downloadfile2")
						end
				end
end


function PANEL:CheckFile2()

				if self.List:GetSelected()[1]!=nil then
					local txtfile = self.List:GetSelected()[1]:GetValue(1)
					
						if(string.find(txtfile, "/") or string.find(txtfile, [[\]]))then

								if(string.find(txtfile, [[\]]))then
								
									local newpath = string.Explode("/", self.Path2)
									self.Path2=""
									local max = #newpath
									
										if(string.find(newpath[max], ".txt"))then
											max = max-1
										end
										
										for k,v in pairs(newpath)do
											if(k<max)then
												if(k==max-1)then
													self.Path2 = self.Path2..v
												else
													self.Path2 = self.Path2..v.."/"
												end
											end
										end

									self:Populate(self.Path2)
								else
									local newpath = string.Explode("/", self.Path2)

									local max = #newpath

										if(string.find(newpath[max], ".txt"))then
											self.Path2=""
												for k,v in pairs(newpath)do
													if(k<max)then
														if(k==max-1)then
															self.Path2 = self.Path2..v
														else
															self.Path2 = self.Path2..v.."/"
														end
													end
												end
										end
									txtfile = string.Left(txtfile, string.len(txtfile)-3)
									self.Path2 = self.Path2..txtfile

									self:Populate(self.Path2)
								end
						else
						
						local newpath = string.Explode("/", self.Path2)

									local max = #newpath

										if(string.find(newpath[max], ".txt"))then
											self.Path2=""
												for k,v in pairs(newpath)do
													if(k<max)then
														if(k==max-1)then
															self.Path2 = self.Path2..v
														else
															self.Path2 = self.Path2..v.."/"
														end
													end
												end
										end

								self.Path2 = self.Path2.."/"..txtfile..".txt"

								RunConsoleCommand( "tbdupe_OpenDuplication", self.Path2 )
						end
				end
end


/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()	

	self:SetTall( 500 )
	self.Panel:StretchToParent(0,0,0,0)
	self.Panel2:StretchToParent(0,0,0,0)
	//self.Panel3:StretchToParent(0,0,0,0)
	
	self.StoreButton:SizeToContents()
	self.StoreButton:SetSize(self.StoreButton:GetWide()+4, self.StoreButton:GetTall()+4)
	self.StoreButton:AlignRight( 5 )
	self.StoreButton:AlignBottom(self.StoreButton:GetTall()+8)
	
	self.NewName:SetTall( self.StoreButton:GetTall() )
	self.NewName:AlignLeft( 5 );	
	self.NewName:AlignBottom(self.StoreButton:GetTall()+8);	
	self.NewName:StretchRightTo( self.StoreButton, 4 );	
	
	self.OpenButton:SizeToContents()
	self.UploadButton:SizeToContents()
	self.DownloadButton:SizeToContents()
	self.DeleteButton:SizeToContents()
	self.FolderButton:SizeToContents()
	
	self.OpenButton:SetSize(self.DownloadButton:GetWide()+4,self.OpenButton:GetTall()+4)
	self.UploadButton:SetSize(self.DownloadButton:GetWide()+4,self.UploadButton:GetTall()+4)
	self.DeleteButton:SetSize(self.DownloadButton:GetWide()+4,self.DeleteButton:GetTall()+4)
	self.FolderButton:SetSize(self.DownloadButton:GetWide()+12, self.FolderButton:GetTall()+4)
	self.DownloadButton:SetSize(self.DownloadButton:GetWide()+4,self.DownloadButton:GetTall()+4)

	
	self.UploadButton2:SizeToContents()
	self.CancelButton2:SizeToContents()
	self.DeleteButton2:SizeToContents()
	
	self.UploadButton2:SetSize(self.DeleteButton2:GetWide()+16,self.UploadButton2:GetTall()+4)
	self.CancelButton2:SetSize(self.DeleteButton2:GetWide()+16,self.CancelButton2:GetTall()+4)
	self.DeleteButton2:SetSize(self.DeleteButton2:GetWide()+16,self.DeleteButton2:GetTall()+4)

	self.OpenButton:AlignLeft(5)
	self.OpenButton:AlignBottom(3)
	
	self.UploadButton:SetPos(5+self.OpenButton:GetWide(),0)
	self.UploadButton:AlignBottom(3)
	
	self.DownloadButton:SetPos(5+self.OpenButton:GetWide()+self.UploadButton:GetWide(),0)
	self.DownloadButton:AlignBottom(3)
	
	self.DeleteButton:SetPos(5+self.OpenButton:GetWide()+self.UploadButton:GetWide()+self.DownloadButton:GetWide(),0)
	self.DeleteButton:AlignBottom(3)
	
	self.FolderButton:SetPos(5+self.OpenButton:GetWide()+self.UploadButton:GetWide()+self.DownloadButton:GetWide()+self.DeleteButton:GetWide(),0)
	self.FolderButton:AlignBottom(3)
	
	self.UploadButton2:AlignLeft(5)
	self.UploadButton2:AlignBottom(3)
	
	self.CancelButton2:SetPos(5+self.UploadButton2:GetWide(),0)
	self.CancelButton2:AlignBottom(3)
	
	self.DeleteButton2:SetPos(5+self.UploadButton2:GetWide()+self.CancelButton2:GetWide(),0)
	self.DeleteButton2:AlignBottom(3)

	self.List2:StretchToParent( 0, 0, 0, 0 )
	self.List2:StretchBottomTo( self.DeleteButton2, 26 )
	
	self.List:StretchToParent( 0, 0, 0, 0 )
	self.List:StretchBottomTo( self.DeleteButton, 26 )
	self.Panel:SetVisible(true)
	self.Panel2:SetVisible(false)

end

/*---------------------------------------------------------
   Name: Clear
---------------------------------------------------------*/
function PANEL:Clear( )

	self.DuplicateList = {}

end

/*---------------------------------------------------------
   Name: Populate
---------------------------------------------------------*/
function PANEL:Add( name )

	self.DuplicateList[ id ] = name
end

/*---------------------------------------------------------
   Name: Populate
---------------------------------------------------------*/
function PANEL:Populate(start)

	self.List:Clear()
 	RunConsoleCommand( "tbdupe_DoPopulation", start )
	
end

function PANEL:PopulateUpload(start)

	self.List2:Clear()
	if(start!="tbsduplicator")then
		self.List2:AddLine([[\..]])
	end
	
	local files = {}
	local folders = {}

	local folder = file.IsDir(start)
		if folder then
			files = file.Find(start.."/*.txt")
			folders = file.FindDir(start.."/*")
		end

	for k,v in pairs(folders)do
		self.List2:AddLine( "/"..v.."/.." )
	end

		for k,v in pairs(files)do
			self.List2:AddLine( string.gsub(v,".txt","") )
		end
end
